package com.ronipl.formalisticsv2;

import java.util.List;

import com.ronipl.createtabs.R;
import com.ronipl.formalisticsv2.model.valueobjects.IUserActionListItem;
import com.ronipl.formalisticsv2.model.valueobjects.StarredAction;
import com.ronipl.formalisticsv2.utilities.dummy.DummyStarredList;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class FragmentD extends Fragment implements OnClickListener {
	
	public FragmentD() {
		// Required empty public constructor
	}
	
	Button StarredRefreshBtn;
	TextView starredHeaderText;
	ListView list;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		View rootView = inflater.inflate(R.layout.fragment_d, container, false);
		
		StarredRefreshBtn = (Button)rootView.findViewById(R.id.RefreshStarred);
		StarredRefreshBtn.setOnClickListener(this);
		
		Typeface cus_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/opensansregular.ttf");
		starredHeaderText = (TextView)rootView.findViewById(R.id.header_bg_wrapper_starred);
		starredHeaderText.setTypeface(cus_font);
		StarredRefreshBtn.setTypeface(cus_font);
		
		list = (ListView) rootView.findViewById(R.id.list_my_request);
		list.setAdapter(new StarredBaseAdapter(this));
		return rootView;
	}

	@Override
	public void onClick(View arg0) {
		// onclick listener
		RefreshStarredData();
		
	}
	
	public void RefreshStarredData(){
		Toast.makeText(getActivity(), "Refresh handler for Starred", Toast.LENGTH_LONG).show();
	}

}

//Setup the Base Adapter for Starred
class StarredBaseAdapter extends BaseAdapter {
	
	List<IUserActionListItem>list;
	
	Context context;
	
	public StarredBaseAdapter(FragmentD fragmentD){
		
		context = (Context)fragmentD.getActivity();
		
		list = DummyStarredList.getDummyStarredList();
		
	}

	@Override
	public int getCount() {
		
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}
	
	//Setup the View Holder class
	class StarredViewHolder {
		
		TextView header;
		TextView headerInfo;
		TextView headerInfoDataB;
		TextView footerTitle;
		TextView footerContent;
		
		public StarredViewHolder(View view) {
			
			header = (TextView)view.findViewById(R.id.header_data_a);
			headerInfo = (TextView) view.findViewById(R.id.header_info);
			headerInfoDataB = (TextView) view.findViewById(R.id.header_info_data_b);
			footerTitle = (TextView)view.findViewById(R.id.footer_title);
			footerContent = (TextView)view.findViewById(R.id.footer_content);
			
		}
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		//a-sign the View parameter to a row(inflated template)
		View row = convertView;
		//a-sign the class StarredViewHolder to a holder and set it to null.
		StarredViewHolder holder = null;
		
		//Create a codition if the row is null set this inflater else re use that template.
		if(row == null){
			
			//setup the layout inflater.
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// set the row to inflate a layout.
			row = inflater.inflate(R.layout.starred_view_list, parent, false);
			
			//new intance of holder set to your holderclass.
			holder = new StarredViewHolder(row);
			
			//setTag the holder
			row.setTag(holder);
			
			final ImageView StarredBtn = (ImageView)row.findViewById(R.id.starred_item);
			StarredBtn.setClickable(true);
			StarredBtn.setOnClickListener(new StarredAction(StarredBtn));
			
		}
		else 
		{
			
			holder = (StarredViewHolder) row.getTag();
			
		}	
		
		IUserActionListItem data = list.get(position);
		
		holder.header.setText(data.getHeader());
		holder.headerInfo.setText(data.getHeaderInfo());
		holder.headerInfoDataB.setText(data.getHeaderInfoDataB());
		holder.footerTitle.setText(data.getFooterTitle());
		holder.footerContent.setText(data.getFooterContent());
		
		Typeface cus_font = Typeface.createFromAsset(context.getAssets(), "fonts/opensansregular.ttf");
		holder.header.setTypeface(cus_font);
		holder.headerInfo.setTypeface(cus_font);
		holder.headerInfoDataB.setTypeface(cus_font);
		holder.footerTitle.setTypeface(cus_font);
		holder.footerContent.setTypeface(cus_font);
		
		
		return row;
	}
	
	
	
}
